home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / web-related / apache_1.0.5 / cgi-src / util.c < prev   
C/C++ Source or Header  |  1996-04-19  |  3KB  |  148 lines

  1. #include <stdio.h>
  2.  
  3. #define LF 10
  4. #define CR 13
  5.  
  6. void getword(char *word, char *line, char stop) {
  7.     int x = 0,y;
  8.  
  9.     for(x=0;((line[x]) && (line[x] != stop));x++)
  10.         word[x] = line[x];
  11.  
  12.     word[x] = '\0';
  13.     if(line[x]) ++x;
  14.     y=0;
  15.  
  16.     while(line[y++] = line[x++]);
  17. }
  18.  
  19. char *makeword(char *line, char stop) {
  20.     int x = 0,y;
  21.     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
  22.  
  23.     for(x=0;((line[x]) && (line[x] != stop));x++)
  24.         word[x] = line[x];
  25.  
  26.     word[x] = '\0';
  27.     if(line[x]) ++x;
  28.     y=0;
  29.  
  30.     while(line[y++] = line[x++]);
  31.     return word;
  32. }
  33.  
  34. char *fmakeword(FILE *f, char stop, int *cl) {
  35.     int wsize;
  36.     char *word;
  37.     int ll;
  38.  
  39.     wsize = 102400;
  40.     ll=0;
  41.     word = (char *) malloc(sizeof(char) * (wsize + 1));
  42.  
  43.     while(1) {
  44.         word[ll] = (char)fgetc(f);
  45.         if(ll==wsize) {
  46.             word[ll+1] = '\0';
  47.             wsize+=102400;
  48.             word = (char *)realloc(word,sizeof(char)*(wsize+1));
  49.         }
  50.         --(*cl);
  51.         if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
  52.             if(word[ll] != stop) ll++;
  53.             word[ll] = '\0';
  54.             return word;
  55.         }
  56.         ++ll;
  57.     }
  58. }
  59.  
  60. char x2c(char *what) {
  61.     register char digit;
  62.  
  63.     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
  64.     digit *= 16;
  65.     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
  66.     return(digit);
  67. }
  68.  
  69. void unescape_url(char *url) {
  70.     register int x,y;
  71.  
  72.     for(x=0,y=0;url[y];++x,++y) {
  73.         if((url[x] = url[y]) == '%') {
  74.             url[x] = x2c(&url[y+1]);
  75.             y+=2;
  76.         }
  77.     }
  78.     url[x] = '\0';
  79. }
  80.  
  81. void plustospace(char *str) {
  82.     register int x;
  83.  
  84.     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  85. }
  86.  
  87. int rind(char *s, char c) {
  88.     register int x;
  89.     for(x=strlen(s) - 1;x != -1; x--)
  90.         if(s[x] == c) return x;
  91.     return -1;
  92. }
  93.  
  94. int getline(char *s, int n, FILE *f) {
  95.     register int i=0;
  96.  
  97.     while(1) {
  98.         s[i] = (char)fgetc(f);
  99.  
  100.         if(s[i] == CR)
  101.             s[i] = fgetc(f);
  102.  
  103.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  104.             s[i] = '\0';
  105.             return (feof(f) ? 1 : 0);
  106.         }
  107.         ++i;
  108.     }
  109. }
  110.  
  111. void send_fd(FILE *f, FILE *fd)
  112. {
  113.     int num_chars=0;
  114.     char c;
  115.  
  116.     while (1) {
  117.         c = fgetc(f);
  118.         if(feof(f))
  119.             return;
  120.         fputc(c,fd);
  121.     }
  122. }
  123.  
  124. int ind(char *s, char c) {
  125.     register int x;
  126.  
  127.     for(x=0;s[x];x++)
  128.         if(s[x] == c) return x;
  129.  
  130.     return -1;
  131. }
  132.  
  133. void escape_shell_cmd(char *cmd) {
  134.     register int x,y,l;
  135.  
  136.     l=strlen(cmd);
  137.     for(x=0;cmd[x];x++) {
  138.         if(ind("&;`'\"|*?~<>^()[]{}$\\\n",cmd[x]) != -1){
  139.             for(y=l+1;y>x;y--)
  140.                 cmd[y] = cmd[y-1];
  141.             l++; /* length has been increased */
  142.             cmd[x] = '\\';
  143.             x++; /* skip the character */
  144.         }
  145.     }
  146. }
  147.  
  148.